package com.cs.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cs.entity.Result;
import com.cs.pojo.User;
import com.cs.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @Reference(timeout = 600000)
    private UserService userService;

    /**
     * 根据id获取用户实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findUserById.do")
    public User findUserById(Integer id) {
        return userService.findUserById(id);
    }


    @RequestMapping("/findUserId.do")
    public Result findUserId(HttpServletRequest request) {
        Result result = null;
        try {
            User user = (User) request.getSession().getAttribute("user");
            result = new Result(true, "查询成功", user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, "查询失败" + e.getMessage());
        }
        return result;
    }


    /***
     * @description 用户登录方法
     *
     * @param user
     * @date 2019/6/20 21:58
     * @return com.cs.entity.Result
     */
    @RequestMapping("/login.do")
    public Result login(@RequestBody User user, HttpServletRequest request) {
        Result result = null;
        try {
            result = userService.login(user);
            if (result.isSuccess()) {
                request.getSession().setAttribute("user", result.getData());
            }
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    /***
     * @description 用户注册
     *
     * @param user
     * @date 2019/6/20 21:42
     * @return com.cs.entity.Result
     */
    @RequestMapping("/regist.do")
    public Result regist(@RequestBody User user) {
        Result result = null;
        try {
            result = userService.regist(user);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    /***
     * @description 修改用户信息
     *
     * @param user
     * @date 2019/6/20 21:44
     * @return com.cs.entity.Result
     */
    @RequestMapping("/update.do")
    public Result update(@RequestBody User user) {
        Result result = null;
        try {
            result = userService.update(user);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    /***
     * @description 获得所有学生列表
     *
     * @param
     * @date 2019/6/20 21:45
     * @return java.util.List<com.cs.pojo.User>
     */
    @RequestMapping("/getStudents.do")
    public List<User> getStudents() {
        return userService.getStudents();
    }

    /***
     * @description 设置为老师权限
     *
     * @param id
     * @date 2019/6/20 21:47
     * @return com.cs.entity.Result
     */
    @RequestMapping("/updateIdentity.do")
    public Result updateIdentity(Integer id) {
        Result result = null;
        try {
            result = userService.updateIdentity(id);
        } catch (Exception e) {
            result = new Result(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping("/showName.do")
    public Result showName(HttpServletRequest request) {
        Result result = null;
        try {
            User user = (User) request.getSession().getAttribute("user");
            result = new Result(true, "查询成功", user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, "查询失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("/logout.do")
    public Result logout(HttpServletRequest request) {
        Result result = null;
        try {
            request.getSession().removeAttribute("user");
            result = new Result(true, "注销成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, "注销失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("/findUserFuzzy.do")
    public List<User> findUserFuzzy(@RequestBody User user) {
        List<User> users = null;
        try {
            users = userService.findUserFuzzy(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

}
